import nattyFetch from 'natty-fetch';
import { urlPrefix, mockUrlPrefix } from './variables';

import { Toast } from 'antd-mobile';

// See https://github.com/Jias/natty-fetch for more details.
const context = nattyFetch.context({
  mockUrlPrefix: mockUrlPrefix,
  urlPrefix: urlPrefix,
  mock: false,
  withCredentials: false,
  traditional: true,
  data: {
    _tb_token_: '',
  },
  timeout: 5000,
  didFetch: () => Toast.hide(),
  // 请按照需要开启
  fit: function (response) {

    let ret = {
      success: response.success
    };
    if (ret.success) {
      ret.content = response.data;
    } else {
      ret.error = {
        message: response.msg
      }
    }
    return ret;
  }
});

context.create('DingTalkAPI', {
  getSomeInfo: {
    mockUrl: 'query/getSomeInfo.json',
    url: 'query/getSomeInfo.json',
    willFetch() {
      Toast.show({
        type: 'loading',
        content: 'Loading',
      });
    },
  },

  //获取钉钉配置
  getConfig: {
    url: 'dingtalk/config/get',
    method: 'POST',
    header: {
      // `POST`、`PUT`或`PATCH`请求的最佳实战推荐设置
      'Content-Type': 'application/json;charset=utf-8'
    },
  },

  //获取钉钉用户信息
  getUser: {
    mock: false,
    url: 'dingtalk/userinfo/get',
    method: 'POST',
    header: {
      // `POST`、`PUT`或`PATCH`请求的最佳实战推荐设置
      'Content-Type': 'application/json;charset=utf-8'
    },
    storage: {
      type: 'localStorage',  // 缓存方式, 默认为'localStorage'
      key: 'sskj-dingtalk-user',     // !!! 唯一必选的参数, 用于内部存储 !!!
      tag: 'v1.0.0.0',          // 缓存的标记, 用于判断是否有效
      duration: 1000 * 60 * 1000,      // 缓存的有效期长, 以毫秒数指定
    }
  },

  //查询工程列表
  getEng: {
    mock: false,
    method: 'GET',
    url: 'jd/eng/page',
  },

});


export default context.api;
